<?php
class Lists extends Base{
	/*class:	列表设计类
	*author:	corrie
	*description:	创造和设计各种列表（信息列表，操作列表，首页列表等）
	*/
	var $_sql;
	var $_per;
	var $_page;
	var $_query;
	var $_data;
	var $_array;
	var $_total;
	var $_pages;
	
	function Lists(){
		//初始化
	}
	function _set($sql, $per=''){
		$this->_sql = $sql;
		$this->_per = $per ? $per : 20;
	}
	function fetchpage($sql='', $per='', $p=''){
		global $db;
		if(!$p)$p = $_REQUEST['p'] ? $_REQUEST['p'] : 1;
		//$this->_set($sql, $per);
		$sql = $this->_sql;
		$per = $this->_per;
		
		$tsql = preg_replace("/^SELECT (.*) FROM /i","SELECT COUNT(*) as count FROM ", $sql);		
		$tdata = $db->query_first($tsql);
		$this->_total = $total = $tdata['count'];
		$this->_pages = $pages = floor($total/($this->_per))+1;
		//echo $pages;
		
		$startp = 1;
		$endp = $pages > $startp + 10 ? $startp + 10 : $pages;
		$html = "<div class='lists_pages'><a href='".$this->pagelink(($p-1)<1?1:$p)."'>上一页</a>";
		for($i=$startp; $i<=$endp; $i++){
			if($i==$p){
				$addclass = " hl";
			}else{
				$addclass = "";
			}
			$html.= "<a href='".$this->pagelink($i)."' class='page_lump{$addclass}'>{$i}</a>";
		}
		$html.= "<a href='".$this->pagelink(($p+1)>$pages?$pages:($p+1))."'>下一页</a></div>";
		$this->_page = $html;
		return $html;
	}
	
	function fetchdata($sql='', $per='', $p=''){
		global $db;
		if(!$p)$p = $_REQUEST['p'] ? $_REQUEST['p'] : 1;
		//$this->_set($sql, $per);
		$sql = $this->_sql;
		$per = $this->_per;	
		
		if($per!='fullpage'){
			$start = ($p-1) * $per;
		}
		$per != 'fullpage' && $query = $db->query($sql." limit $start,$per");
		$per == 'fullpage' && $query = $db->query($sql);
		$this->_query = $query;
		while($getdata = $db->fetch_array($query)){
			$datas[] = $getdata;
		}
		$this->_data = $datas;
		return $datas;
		/*
			You can format it the standard mode use while(data){};
			Then Use the table maker to design a table with page bar;
		*/
	}
	
	function pagelink($page, $pagefile='', $p=''){
		if(!$p)$p = 'p';
		if(!$pagefile){
			$uarray = pathinfo($_SERVER['PHP_SELF']);
			$pagefile = $uarray['basename'];
		}
		
		$query_string = $_SERVER['QUERY_STRING'];
		if(!$query_string){
			$url = $pagefile."?{$p}={$page}";
		}else{
			$exp = "/p=([0-9]{1,})/i";
			if(preg_match($exp, $query_string)){
				$url = $pagefile.'?'.preg_replace($exp, "{$p}={$page}", $query_string);	
			}else{
				$url = $pagefile.'?'.$query_string."&{$p}={$page}";
			}
		}
		return $url;
	}
	//生成基本表格框架的函数
	function createtable($data, $head='', $width='', $sort='', $add=''){
		foreach($data as $k=>$v){
			$length = count($v);
			break;
		}
		$list = '<div class="lists" '.$add.'>';
		if(!empty($head) && $head){
			for($i=0; $i<count($head); $i++){
				if(count($head)<$length){
					$head[] = '&nbsp;';
				}elseif(count($head)>$length){
					$keys = array_keys($head);
					$lastkey = $keys[count($keys)-1];
					unset($head[$lastkey]);
				}
			}
		}
		if(!empty($width) && $width){
			for($i=0; $i<count($width); $i++){
				if(count($width)<$length){
					$width[] = '';
				}elseif(count($width)>$length){
					$keys = array_keys($width);
					$lastkey = $keys[count($keys)-1];
					unset($width[$lastkey]);
				}
			}
		}
		$ndata = array();
		$nwidth = array();
		$nhead = array();
		if($sort && !empty($sort)){
			$i = 0;			
			foreach($sort as $k=>$v){
				//$ndata[$i] = $data[$v];
				foreach($data as $key=>$value){
					$ndata[$key][$i] = $data[$key][$v];
				}
				$width && $nwidth[$i] = $width[$v];
				$head && $nhead[$i] = $head[$v];
				$i++;
			}
			if(count($sort)<$length){
				//补齐
			}
		}
		empty($ndata) && $ndata = $data;
		empty($nwidth) && $nwidth = $width;
		empty($nhead) && $nhead = $head;

		if(!empty($nhead) && $nhead){
			$list.= '<div class="list_head">';
			$i = 1;
			foreach($nhead as $k=>$v){
				if(isset($nwidth[$k]))$wh = "style='width:{$nwidth[$k]}'";
				else $wh='';
				$list.= '<div class="list_lump list_col_'.$i.'"'.$wh.'><div>'.$v.'</div></div>';
				$i++;
			}
			$list.= '<div class="clear"></div></div>'."\n";
		}
		foreach($ndata as $key=>$value){
			$addclass = '';
			if(($key+1)%2==0)
				$addclass = ' odd';
			if($key==0){
				$addclass.= ' first';
			}
			$list.= '<div class="list_line'.$addclass.'">';
			$i = 1;
			foreach($value as $k=>$v){
				if(isset($nwidth[$k]))$wh = " style='width:{$nwidth[$k]}'";
				else $wh='';
				$list.= '<div class="list_lump list_col_'.$i.'"'.$wh.'><div>'.$v.'</div></div>';
				$i++;
			}
			$list.= "<div class='clear'></div></div>\n";
		}
		$list.= "</div>";
		return $list;		
	}
	
	function handledata($datas='', $func=''){
		$datas = $datas ? $datas : $this->_data;
		$func = $func ? $func : "handledataarray";
		//
		$this->_array = $array = $func($datas);
		return $array;
	}
	
	function datalist(){
	}
}
?>